Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports MigraDoc.DocumentObjectModel
Imports MigraDoc.Rendering
Imports System.IO

Public Class frm<OwnerU>_<TableU>
    Inherits System.Web.UI.UserControl

    Private cls<OwnerU>_<TableU>Data As New <OwnerU>_<TableU>DataClass
    Private cls<Database>Data As New <Database>DataClass
    Private dv<OwnerU>_<TableU> As DataView

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Session("Det_Mode") = ""
    <If Loop>
        <IsPKey>
            Session("Det_<FieldU>") = ""
    </If Loop>
    <If Loop>
        <IsNotPKey>
        <And>
        <IsIdentity>
            Session("Det_<FieldU>") = ""
        <ExitLoop>
    </If Loop>
    <If Loop>
        <IsNotIdentityCount>
        <And>
        <IsNotPKeyCount>
            Session("Det_<FieldU>") = ""
        <ExitLoop>
    </If Loop>

            Session.Remove("dv<OwnerU>_<TableU>")

    <If Loop>
   	<IsNotText>
   	<And>
   	<IsNotNText>
            cmbFields.Items.Add("<FieldC>")
    </If Loop>

            cmbCondition.Items.Add("Contains")
            cmbCondition.Items.Add("Equals")
            cmbCondition.Items.Add("Starts with...")
            cmbCondition.Items.Add("More than...")
            cmbCondition.Items.Add("Less than...")
            cmbCondition.Items.Add("Equal or more than...")
            cmbCondition.Items.Add("Equal or less than...")

            cmbRecords.Items.Add("5")
            cmbRecords.Items.Add("10")
            cmbRecords.Items.Add("25")
            cmbRecords.Items.Add("50")
            cmbRecords.Items.Add("100")
            cmbRecords.Items.Add("500")

        <If Loop>
          <IsFKey>
           <And>
           <IsComboBox (Db)>
            Load<OwnerU>_<TableU>_<ROwnerU>_<RTableU>ComboBox<MiscTag>()
        </If Loop>
            LoadGrid<OwnerU>_<TableU>()
        End If
    End Sub

    <If Loop>
      <IsFKey>
      <And>
      <IsComboBox (Db)>
    Private Sub Load<OwnerU>_<TableU>_<ROwnerU>_<RTableU>ComboBox<MiscTag>()
        Dim <OwnerU>_<TableU>_<ROwnerU>_<RTableU>List As List(Of <OwnerU>_<TableU>_<ROwnerU>_<RTableU>Class<MiscTag>)
        Try
            <OwnerU>_<TableU>_<ROwnerU>_<RTableU>List = <OwnerU>_<TableU>_<ROwnerU>_<RTableU>DataClass<MiscTag>.List
            <IsFKey>
            <And>
            <IsComboBox (Db)>
            txt<FieldU>.DataSource = <OwnerU>_<TableU>_<ROwnerU>_<RTableU>List
            txt<FieldU>.DataValueField = "<RFieldU>"
            txt<FieldU>.DataTextField = "<RDisplayFieldU>"
            txt<FieldU>.DataBind()
        Catch ex As Exception
	    ec.ShowMessage(ex.Message, " <Head> ")
        End Try
    End Sub

    </If Loop>
    Public Sub UnLoadAll()
        With grd<TableU>
            .DataSource = Nothing
            .DataBind()
        End With
        pnlGrid.Visible = True
        pnlForm.Visible = False
        pnlDelete.Visible = False
        pnlSave.Visible = False
        pnlMessage.Visible = False
    End Sub

    Public Sub LoadGrid<OwnerU>_<TableU>()
	Try
            If Not IsNothing(Session("dv<OwnerU>_<TableU>")) Then
                dv<OwnerU>_<TableU> = CType(Session("dv<OwnerU>_<TableU>"), DataView)
            Else ' DataView was not found in the Session.
            	dv<OwnerU>_<TableU> = cls<OwnerU>_<TableU>Data.Select_Records(System.Convert.To<DRType>(Session("Det_<DRField>"))).DefaultView
            	Session("dv<OwnerU>_<TableU>") = dv<OwnerU>_<TableU>
            End If
            If dv<OwnerU>_<TableU>.Count > 0 Then
                dv<OwnerU>_<TableU>.Sort = htmlHiddenSortExpression.Value
                With grd<TableU>
                    .DataSource = dv<OwnerU>_<TableU>
                    .DataBind()
                End With
            Else
                With grd<TableU>
                    .DataSource = Nothing
                    .DataBind()
                End With
            End If
        Catch ex As Exception
	    ec.ShowMessage(ex.Message, " <Head> ")
        End Try
    End Sub

    Private Sub Add()
        Session("Det_Mode") = "Add"

        ClearRecord()

        <If Loop>
           <IsNotIdentity>
           <And>
           <IsCheckBox>
        Me.txt<FieldU>.Enabled = True
           <IsNotIdentity>
           <And>
           <IsNumericUpDown>
        Me.txt<FieldU>.Enabled = True
           <IsNotIdentity>
           <And>
           <IsDateTimePicker>
        Me.txt<FieldU>.Enabled = True
           <IsNotIdentity>
           <And>
           <IsTextBox>
        Me.txt<FieldU>.Enabled = True
           <IsNotIdentity>
           <And>
           <IsMaskedTextBox>
        Me.txt<FieldU>.Enabled = True
           <IsNotIdentity>
           <And>
           <IsMultilineTextBox>
        Me.txt<FieldU>.Enabled = True
           <IsNotIdentity>
           <And>
           <IsRichTextBox>
        Me.txt<FieldU>.Enabled = True
           <IsNotIdentity>
           <And>
           <IsPictureBox>
        Me.txt<FieldU>.Enabled = True
           <IsNotIdentity>
           <And>
           <IsComboBox>
        Me.txt<FieldU>.Enabled = True
           <IsNotIdentity>
           <And>
           <IsComboBox (Db)>
        Me.txt<FieldU>.Enabled = True
        </If Loop>
 -----------------------------------------------------<Comment>
        <If Loop>
           <IsIdentity>
           <And>
           <IsNumericUpDown>
        txt<FieldU>.Enabled = False
           <IsIdentity>
           <And>
           <IsTextBox>
        txt<FieldU>.Enabled = False
           <IsIdentity>
           <And>
           <IsMaskedTextBox>
        txt<FieldU>.Enabled = False
           <IsIdentity>
           <And>
           <IsMultilineTextBox>
        txt<FieldU>.Enabled = False
           <IsIdentity>
           <And>
           <IsRichTextBox>
        txt<FieldU>.Enabled = False
           <IsIdentity>
           <And>
           <IsComboBox>
        txt<FieldU>.Enabled = False
           <IsIdentity>
           <And>
           <IsComboBox (Db)>
        txt<FieldU>.Enabled = False
        </If Loop>
        <If Loop>
           <IsNotIdentity>
           <And>
           <IsCheckBox>
        txt<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsNumericUpDown> 
        txt<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsDateTimePicker>
        txt<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsTextBox>
        txt<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsMaskedTextBox>
        txt<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsMultilineTextBox>
        txt<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsRichTextBox>
        txt<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsPictureBox>
        txt<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsComboBox>
        txt<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsComboBox (Db)>
        txt<FieldU>.Focus()
              <ExitLoop>
        </If Loop>
-----------------------------------------------------<Comment>
        <If Loop>
           <IsIdentity>
           <And>
           <IsNumericUpDown>
        txt<FieldU>.Text = System.Convert.ToString(cls<Database>Data.getAutoID("New", "<Table>"))
        </If Loop>
-----------------------------------------------------<Comment>
        <If Loop>
           <IsFKey>
           <And>
           <IsNotComboBox (Db)>
        txt<FieldU>.Enabled = False
        txt<FieldU>.Text = CStr(Session("Det_<DRField>"))
        </If Loop>
    End Sub

    Private Sub GetData()
        ClearRecord()

        Dim cls<OwnerU>_<TableU> As New <OwnerU>_<TableU>Class
        <If Loop>
           <IsPKey>
        cls<OwnerU>_<TableU>.<FieldU> = <Convert>(Session("Det_<FieldU>"))
        </If Loop>
        <If Loop>
           <IsNotPKey>
           <And>
           <IsIdentity>
        cls<OwnerU>_<TableU>.<FieldU> = <Convert>(Session("Det_<FieldU>"))
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
        cls<OwnerU>_<TableU>.<FieldU> = <Convert>(Session("Det_<FieldU>"))
           <ExitLoop>
        </If Loop>
        cls<OwnerU>_<TableU> = cls<OwnerU>_<TableU>Data.Select_Record(cls<OwnerU>_<TableU>)

        If Not cls<OwnerU>_<TableU> Is Nothing Then
            Try
                <If Loop>
                   <IsCheckBox>
                   <And>
                   <IsNull>
                txt<FieldU>.Checked = If(cls<OwnerU>_<TableU>.<Field> Is Nothing, Nothing, <Convert>(cls<OwnerU>_<TableU>.<Field>))
                   <IsNumericUpDown>
                   <And>
                   <IsNull>
                txt<FieldU>.Text = If(cls<OwnerU>_<TableU>.<Field> Is Nothing, Nothing, System.Convert.ToString(cls<OwnerU>_<TableU>.<Field>))
                   <IsDateTimePicker>
 	           <And>
                   <IsNotDateTimeOffset>
 	           <And>
                   <IsNotTime>
                   <And>
                   <IsNull>
                txt<FieldU>.Text = If(cls<OwnerU>_<TableU>.<Field> Is Nothing, Date.Now.ToString, System.Convert.ToDateTime(cls<OwnerU>_<TableU>.<Field>).ToShortDateString)
                   <IsDateTimePicker>
 	           <And>
                   <IsDateTimeOffset>
 	           <And>
                   <IsNotTime>
                   <And>
                   <IsNull>
                txt<FieldU>.Text = If(cls<OwnerU>_<TableU>.<Field> Is Nothing, Date.Now.ToString, System.Convert.ToString(cls<OwnerU>_<TableU>.<Field>.DateTime))
                   <IsDateTimePicker>
 	           <And>
                   <IsNotDateTimeOffset>
 	           <And>
                   <IsTime>
                   <And>
                   <IsNull>
                txt<FieldU>.Text = If(cls<OwnerU>_<TableU>.<Field> Is Nothing, Date.Now.ToString, System.Convert.ToString(cls<OwnerU>_<TableU>.<Field>.ToString))
                   <IsTextBox>
                   <And>
                   <IsNull>
                txt<FieldU>.Text = If(cls<OwnerU>_<TableU>.<Field> Is Nothing, Nothing, System.Convert.ToString(cls<OwnerU>_<TableU>.<Field>))
                   <IsMaskedTextBox>
                   <And>
                   <IsNull>
                txt<FieldU>.Text = If(cls<OwnerU>_<TableU>.<Field> Is Nothing, Nothing, System.Convert.ToString(cls<OwnerU>_<TableU>.<Field>))
                   <IsMultilineTextBox>
                   <And>
                   <IsNull>
                txt<FieldU>.Text = If(cls<OwnerU>_<TableU>.<Field> Is Nothing, Nothing, System.Convert.ToString(cls<OwnerU>_<TableU>.<Field>))
                   <IsRichTextBox>
                   <And>
                   <IsNull>
                txt<FieldU>.Text = If(cls<OwnerU>_<TableU>.<Field> Is Nothing, Nothing, System.Convert.ToString(cls<OwnerU>_<TableU>.<Field>))
                   <IsPictureBox>
                   <And>
                   <IsNull>
                txt<FieldU>.Text = If(cls<OwnerU>_<TableU>.<Field> Is Nothing, Nothing, cls<OwnerU>_<TableU>.<Field>)
                   <IsComboBox>
                   <And>
                   <IsNull>
                txt<FieldU>.Text = If(cls<OwnerU>_<TableU>.<Field> Is Nothing, Nothing, System.Convert.ToString(cls<OwnerU>_<TableU>.<Field>))
                   <IsComboBox (Db)>
                   <And>
                   <IsNull>
                txt<FieldU>.SelectedValue = If(cls<OwnerU>_<TableU>.<Field> Is Nothing, Nothing, System.Convert.ToString(cls<OwnerU>_<TableU>.<Field>))
--------------------------------------------------------------------------------------------------------<Comment>
                   <IsCheckBox>
                   <And>
                   <IsNotNull>
                txt<FieldU>.Checked = <Convert>(cls<OwnerU>_<TableU>.<Field>)
                   <IsNumericUpDown>
                   <And>
                   <IsNotNull>
                txt<FieldU>.Text = System.Convert.ToString(cls<OwnerU>_<TableU>.<Field>)
                   <IsDateTimePicker>
 	           <And>
                   <IsNotDateTimeOffset>
 	           <And>
                   <IsNotTime>
                   <And>
                   <IsNotNull>
                txt<FieldU>.Text = System.Convert.ToDateTime(cls<OwnerU>_<TableU>.<Field>).ToShortDateString
                   <IsDateTimePicker>
 	           <And>
                   <IsDateTimeOffset>
 	           <And>
                   <IsNotTime>
                   <And>
                   <IsNotNull>
                txt<FieldU>.Text = System.Convert.ToString(cls<OwnerU>_<TableU>.<Field>.DateTime)
                   <IsDateTimePicker>
 	           <And>
                   <IsNotDateTimeOffset>
 	           <And>
                   <IsTime>
                   <And>
                   <IsNotNull>
                txt<FieldU>.Text = System.Convert.ToString(cls<OwnerU>_<TableU>.<Field>.ToString)
                   <IsTextBox>
                   <And>
                   <IsNotNull>
                txt<FieldU>.Text = System.Convert.ToString(cls<OwnerU>_<TableU>.<Field>)
                   <IsMaskedTextBox>
                   <And>
                   <IsNotNull>
                txt<FieldU>.Text = System.Convert.ToString(cls<OwnerU>_<TableU>.<Field>)
                   <IsMultilineTextBox>
                   <And>
                   <IsNotNull>
                txt<FieldU>.Text = System.Convert.ToString(cls<OwnerU>_<TableU>.<Field>)
                   <IsRichTextBox>
                   <And>
                   <IsNotNull>
                txt<FieldU>.Text = System.Convert.ToString(cls<OwnerU>_<TableU>.<Field>)
                   <IsPictureBox>
                   <And>
                   <IsNotNull>
                txt<FieldU>.Text = cls<OwnerU>_<TableU>.<Field>
                   <IsComboBox>
                   <And>
                   <IsNotNull>
                txt<FieldU>.Text = System.Convert.ToString(cls<OwnerU>_<TableU>.<Field>)
                   <IsComboBox (Db)>
                   <And>
                   <IsNotNull>
                txt<FieldU>.SelectedValue = System.Convert.ToString(cls<OwnerU>_<TableU>.<Field>)
                </If Loop>
            Catch
            End Try
        End If
    End Sub

    Private Sub Edit()
        Session("Det_Mode") = "Edit"

        GetData()
        
        <If Loop>
           <IsNotIdentity>
           <And>
           <IsNotPKey>
           <And>
           <IsNumericUpDown>
        txt<FieldU>.Enabled = True
           <IsNotIdentity>
           <And>
           <IsNotPKey>
           <And>
           <IsTextBox>
        txt<FieldU>.Enabled = True
           <IsNotIdentity>
           <And>
           <IsNotPKey>
           <And>
           <IsMaskedTextBox>
        txt<FieldU>.Enabled = True
           <IsNotIdentity>
           <And>
           <IsNotPKey>
           <And>
           <IsMultilineTextBox>
        txt<FieldU>.Enabled = True
           <IsNotIdentity>
           <And>
           <IsNotPKey>
           <And>
           <IsRichTextBox>
        txt<FieldU>.Enabled = True
           <IsNotIdentity>
           <And>
           <IsNotPKey>
           <And>
           <IsComboBox (Db)>
        txt<FieldU>.Enabled = True
        </If Loop>
        <If Loop>
           <IsIdentity>
           <And>
           <IsNumericUpDown>
        txt<FieldU>.Enabled = False
           <IsIdentity>
           <And>
           <IsTextBox>
        txt<FieldU>.Enabled = False
           <IsIdentity>
           <And>
           <IsMaskedTextBox>
        txt<FieldU>.Enabled = False
           <IsIdentity>
           <And>
           <IsMultilineTextBox>
        txt<FieldU>.Enabled = False
           <IsIdentity>
           <And>
           <IsRichTextBox>
        txt<FieldU>.Enabled = False
           <IsIdentity>
           <And>
           <IsComboBox>
        txt<FieldU>.Enabled = False
           <IsIdentity>
           <And>
           <IsComboBox (Db)>
        txt<FieldU>.Enabled = False
        </If Loop>
        <If Loop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsNumericUpDown>
        txt<FieldU>.Enabled = False
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsTextBox>
        txt<FieldU>.Enabled = False
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsMaskedTextBox>
        txt<FieldU>.Enabled = False
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsMultilineTextBox>
        txt<FieldU>.Enabled = False
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsRichTextBox>
        txt<FieldU>.Enabled = False
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsComboBox>
        txt<FieldU>.Enabled = False
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsComboBox (Db)>
        txt<FieldU>.Enabled = False
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsNumericUpDown>
        txt<FieldU>.Enabled = False
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsTextBox>
        txt<FieldU>.Enabled = False
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsMaskedTextBox>
        txt<FieldU>.Enabled = False
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsMultilineTextBox>
        txt<FieldU>.Enabled = False
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsRichTextBox>
        txt<FieldU>.Enabled = False
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsComboBox (Db)>
        txt<FieldU>.Enabled = False
           <ExitLoop>
        </If Loop>
        <If Loop>
           <IsNotIdentity>
           <And>
           <IsNotPKey>
           <And>
           <IsCheckBox>
        txt<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsNotPKey>
           <And>
           <IsNumericUpDown>
        txt<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsNotPKey>
           <And>
           <IsDateTimePicker>
        txt<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsNotPKey>
           <And>
           <IsTextBox>
        txt<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsNotPKey>
           <And>
           <IsMaskedTextBox>
        txt<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsNotPKey>
           <And>
           <IsMultilineTextBox>
        txt<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsNotPKey>
           <And>
           <IsRichTextBox>
        txt<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsNotPKey>
           <And>
           <IsComboBox>
        txt<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsNotPKey>
           <And>
           <IsComboBox (Db)>
        txt<FieldU>.Focus()
           <ExitLoop>
        </If Loop>
-----------------------------------------------------<Comment>
        <If Loop>
           <IsFKey>
           <And>
           <IsNotComboBox (Db)>
        txt<FieldU>.Enabled = False
        </If Loop>
    End Sub

    Private Sub Delete()
        Session("Det_Mode") = "Delete"

        GetData()

        <If Loop>
           <IsCheckBox>
        Me.txt<FieldU>.Enabled = False
           <IsNumericUpDown>
        Me.txt<FieldU>.Enabled = False
           <IsDateTimePicker>
        Me.txt<FieldU>.Enabled = False
           <IsTextBox>
        Me.txt<FieldU>.Enabled = False
           <IsMaskedTextBox>
        Me.txt<FieldU>.Enabled = False
           <IsMultilineTextBox>
        Me.txt<FieldU>.Enabled = False
           <IsRichTextBox>
        Me.txt<FieldU>.Enabled = False
           <IsPictureBox>
        Me.txt<FieldU>.Enabled = False
           <IsComboBox>
        Me.txt<FieldU>.Enabled = False
           <IsComboBox (Db)>
        Me.txt<FieldU>.Enabled = False
        </If Loop>
    End Sub

    Private Sub ClearRecord()
        <If Loop>
           <IsCheckBox>
        Me.txt<FieldU>.Text = Nothing
           <IsNumericUpDown>
        Me.txt<FieldU>.Text = System.Convert.ToString(0)
           <IsDateTimePicker>
        Me.txt<FieldU>.Text = Nothing
           <IsTextBox>
        Me.txt<FieldU>.Text = Nothing
           <IsMaskedTextBox>
        Me.txt<FieldU>.Text = Nothing
           <IsMultilineTextBox>
        Me.txt<FieldU>.Text = Nothing
           <IsRichTextBox>
        Me.txt<FieldU>.Text = Nothing
           <IsPictureBox>
        Me.txt<FieldU>.Text = Nothing
           <IsComboBox>
        Me.txt<FieldU>.SelectedIndex = -1
           <IsComboBox (Db)>
        Me.txt<FieldU>.SelectedIndex = -1
        </If Loop>
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
	If Session("Det_Mode") Is "Add" Then
            Me.InsertRecord()
        ElseIf Session("Det_Mode") Is "Edit" Then
            Me.UpdateRecord()
        End If
    End Sub

    Private Sub SetData(ByVal cls<OwnerU>_<TableU> As <OwnerU>_<TableU>Class)
        With cls<OwnerU>_<TableU>
                <If Loop>
                    <IsNotIdentity>
                    <And>
                    <IsCheckBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
            .<FieldU> = If(txt<FieldU>.Checked, True, False)
                    <IsNotIdentity>
                    <And>
                    <IsNumericUpDown>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
            .<FieldU> = If(String.IsNullOrEmpty(txt<FieldU>.Text), Nothing, CType(txt<FieldU>.Text, <VBType>?))
                    <IsNotIdentity>
                    <And>
                    <IsDateTimePicker>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
            .<FieldU> = If(String.IsNullOrEmpty(txt<FieldU>.Text), Nothing, CType(txt<FieldU>.Text, <VBType>?))
                    <IsNotIdentity>
                    <And>
                    <IsTextBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
            .<FieldU> = If(String.IsNullOrEmpty(txt<FieldU>.Text), Nothing, CType(txt<FieldU>.Text, <VBType>?))
                    <IsNotIdentity>
                    <And>
                    <IsMaskedTextBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
            .<FieldU> = If(String.IsNullOrEmpty(txt<FieldU>.Text), Nothing, CType(txt<FieldU>.Text, <VBType>?))
                    <IsNotIdentity>
                    <And>
                    <IsMultilineTextBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
            .<FieldU> = If(String.IsNullOrEmpty(txt<FieldU>.Text), Nothing, CType(txt<FieldU>.Text, <VBType>?))
                    <IsNotIdentity>
                    <And>
                    <IsRichTextBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
            .<FieldU> = If(String.IsNullOrEmpty(txt<FieldU>.Text), Nothing, CType(txt<FieldU>.Text, <VBType>?))
                    <IsNotIdentity>
                    <And>
                    <IsPictureBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
                          
                    <IsNotIdentity>
                    <And>
                    <IsComboBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
            .<FieldU> = <Convert>(txt<FieldU>.Text)
                    <IsNotIdentity>
                    <And>
                    <IsComboBox (Db)>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
            .<FieldU> = If(String.IsNullOrEmpty(txt<FieldU>.SelectedValue), Nothing, CType(txt<FieldU>.SelectedValue, <VBType>?))
--------------------------------------------------------------------------------------------------------<Comment>
                    <IsNotIdentity>
                    <And>
                    <IsCheckBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
            .<FieldU> = If(txt<FieldU>.Checked, True, False)
                    <IsNotIdentity>
                    <And>
                    <IsNumericUpDown>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
            .<FieldU> = If(String.IsNullOrEmpty(txt<FieldU>.Text), Nothing, CType(txt<FieldU>.Text, <VBType>?))
                    <IsNotIdentity>
                    <And>
                    <IsDateTimePicker>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
            .<FieldU> = If(String.IsNullOrEmpty(txt<FieldU>.Text), Nothing, CType(txt<FieldU>.Text, <VBType>?))
                    <IsNotIdentity>
                    <And>
                    <IsTextBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
            .<FieldU> = If(String.IsNullOrEmpty(txt<FieldU>.Text), Nothing, txt<FieldU>.Text)
                    <IsNotIdentity>
                    <And>
                    <IsMaskedTextBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
            .<FieldU> = If(String.IsNullOrEmpty(txt<FieldU>.Text), Nothing, txt<FieldU>.Text)
                    <IsNotIdentity>
                    <And>
                    <IsMultilineTextBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
            .<FieldU> = If(String.IsNullOrEmpty(txt<FieldU>.Text), Nothing, txt<FieldU>.Text)
                    <IsNotIdentity>
                    <And>
                    <IsRichTextBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
            .<FieldU> = If(String.IsNullOrEmpty(txt<FieldU>.Text), Nothing, txt<FieldU>.Text)
                    <IsNotIdentity>
                    <And>
                    <IsPictureBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
                          
                    <IsNotIdentity>
                    <And>
                    <IsComboBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
            .<FieldU> = <Convert>(txt<FieldU>.Text)
                    <IsNotIdentity>
                    <And>
                    <IsComboBox (Db)>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
            .<FieldU> = If(String.IsNullOrEmpty(txt<FieldU>.SelectedValue), Nothing, txt<FieldU>.SelectedValue.ToString)
--------------------------------------------------------------------------------------------------------<Comment>
                    <IsNotIdentity>
                    <And>
                    <IsCheckBox>
                    <And>
                    <IsNotNull>
            .<FieldU> = <Convert>(IIf(txt<FieldU>.Checked, True, False))
                    <IsNotIdentity>
                    <And>
                    <IsNumericUpDown>
                    <And>
                    <IsNotNull>
            .<FieldU> = <Convert>(txt<FieldU>.Text)
                    <IsNotIdentity>
                    <And>
                    <IsDateTimePicker>
                    <And>
                    <IsNotNull>
            .<FieldU> = <Convert>(txt<FieldU>.Text)
                    <IsNotIdentity>
                    <And>
                    <IsTextBox>
                    <And>
                    <IsNotNull>
            .<FieldU> = <Convert>(txt<FieldU>.Text)
                    <IsNotIdentity>
                    <And>
                    <IsMaskedTextBox>
                    <And>
                    <IsNotNull>
            .<FieldU> = <Convert>(txt<FieldU>.Text)
                    <IsNotIdentity>
                    <And>
                    <IsMultilineTextBox>
                    <And>
                    <IsNotNull>
            .<FieldU> = <Convert>(txt<FieldU>.Text)
                    <IsNotIdentity>
                    <And>
                    <IsRichTextBox>
                    <And>
                    <IsNotNull>
            .<FieldU> = <Convert>(txt<FieldU>.Text)
                    <IsNotIdentity>
                    <And>
                    <IsPictureBox>
                    <And>
                    <IsNotNull>
                          
                    <IsNotIdentity>
                    <And>
                    <IsComboBox>
                    <And>
                    <IsNotNull>
            .<FieldU> = <Convert>(txt<FieldU>.Text)
                    <IsNotIdentity>
                    <And>
                    <IsComboBox (Db)>
                    <And>
                    <IsNotNull>
            .<FieldU> = <Convert>(txt<FieldU>.SelectedValue)
--------------------------------------------------------------------------------------------------------<Comment>
                </If Loop>
        End With
    End Sub

    Private Sub InsertRecord()
        Dim cls<OwnerU>_<TableU> As New <OwnerU>_<TableU>Class
        If VerifyData() = True Then
            SetData(cls<OwnerU>_<TableU>)
            Dim bSucess As Boolean
            bSucess = cls<OwnerU>_<TableU>Data.Add(cls<OwnerU>_<TableU>)
            If bSucess = True Then
                pnlForm.Visible = False
                pnlSave.Visible = False
                pnlGrid.Visible = True
                lblMode.InnerText = ""
                Session.Remove("dv<OwnerU>_<TableU>")
                LoadGrid<OwnerU>_<TableU>()
            Else
                ec.ShowMessage(" Insert failed. ", " <Head> ")
            End If
        End If
    End Sub

    Private Sub UpdateRecord()
        Dim ocls<OwnerU>_<TableU> As New <OwnerU>_<TableU>Class
        Dim cls<OwnerU>_<TableU> As New <OwnerU>_<TableU>Class

        <If Loop>
           <IsPKey>
        ocls<OwnerU>_<TableU>.<FieldU> = <Convert>(Session("Det_<FieldU>"))
        </If Loop>
        <If Loop>
           <IsNotPKey>
           <And>
           <IsIdentity>
        ocls<OwnerU>_<TableU>.<FieldU> = <Convert>(Session("Det_<FieldU>"))
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
        ocls<OwnerU>_<TableU>.<FieldU> = <Convert>(Session("Det_<FieldU>"))
           <ExitLoop>
        </If Loop>
        ocls<OwnerU>_<TableU> = cls<OwnerU>_<TableU>Data.Select_Record(ocls<OwnerU>_<TableU>)

        If VerifyData() = True Then
            SetData(cls<OwnerU>_<TableU>)
            Dim bSucess As Boolean
            bSucess = cls<OwnerU>_<TableU>Data.Update(ocls<OwnerU>_<TableU>, cls<OwnerU>_<TableU>)
            If bSucess = True Then
                pnlForm.Visible = False
                pnlSave.Visible = False
                pnlGrid.Visible = True
                lblMode.InnerText = ""
                Session.Remove("dv<OwnerU>_<TableU>")
                LoadGrid<OwnerU>_<TableU>()
            Else
                ec.ShowMessage(" Update failed. ", " <Head> ")
            End If
        End If
    End Sub

    Private Sub DeleteRecord()
        Dim cls<OwnerU>_<TableU> As New <OwnerU>_<TableU>Class
        <If Loop>
           <IsPKey>
        cls<OwnerU>_<TableU>.<FieldU> = <Convert>(Session("Det_<FieldU>"))
        </If Loop>
        <If Loop>
           <IsNotPKey>
           <And>
           <IsIdentity>
        cls<OwnerU>_<TableU>.<FieldU> = <Convert>(Session("Det_<FieldU>"))
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
        cls<OwnerU>_<TableU>.<FieldU> = <Convert>(Session("Det_<FieldU>"))
           <ExitLoop>
        </If Loop>
        SetData(cls<OwnerU>_<TableU>)
        Dim bSucess As Boolean
        bSucess = cls<OwnerU>_<TableU>Data.Delete(cls<OwnerU>_<TableU>)
        If bSucess = True Then
            pnlForm.Visible = False
            pnlSave.Visible = False
            pnlGrid.Visible = True
            pnlDelete.Visible = False
            lblMode.InnerText = ""
            Session.Remove("dv<OwnerU>_<TableU>")
            LoadGrid<OwnerU>_<TableU>()
        Else
            ec.ShowMessage(" Delete failed. ", " <Head> ")
        End If
    End Sub

    Private Function VerifyData() As Boolean
        <If Loop>
           <IsNotIdentity>
           <And>
           <IsNotNull>
           <And>
           <IsCheckBox>
          
           <IsNotIdentity>
           <And>
           <IsNotNull>
           <And>
           <IsNumericUpDown>
        If txt<FieldU>.Text = "" Then
            ec.ShowMessage(" <FieldC> is Required ",  " <Head> ")
            txt<FieldU>.Focus()
            Return False
        End If
           <IsNotIdentity>
           <And>
           <IsNotNull>
           <And>
           <IsDateTimePicker>
        
           <IsNotIdentity>
           <And>
           <IsNotNull>
           <And>
           <IsTextBox>
        If txt<FieldU>.Text = "" Then
            ec.ShowMessage(" <FieldC> is Required ",  " <Head> ")
            txt<FieldU>.Focus()
            Return False
        End If
           <IsNotIdentity>
           <And>
           <IsNotNull>
           <And>
           <IsMaskedTextBox>
        If txt<FieldU>.Text = "" Then
            ec.ShowMessage(" <FieldC> is Required ",  " <Head> ")
            txt<FieldU>.Focus()
            Return False
        End If
           <IsNotIdentity>
           <And>
           <IsNotNull>
           <And>
           <IsMultilineTextBox>
        If txt<FieldU>.Text = "" Then
            ec.ShowMessage(" <FieldC> is Required ",  " <Head> ")
            txt<FieldU>.Focus()
            Return False
        End If
           <IsNotIdentity>
           <And>
           <IsNotNull>
           <And>
           <IsRichTextBox>
        If txt<FieldU>.Text = "" Then
            ec.ShowMessage(" <FieldC> is Required ",  " <Head> ")
            txt<FieldU>.Focus()
            Return False
        End If
           <IsNotIdentity>
           <And>
           <IsNotNull>
           <And>
           <IsPictureBox>
        
           <IsNotIdentity>
           <And>
           <IsNotNull>
           <And>
           <IsComboBox>
        If txt<FieldU>.SelectedIndex = -1 Then
            ec.ShowMessage(" <FieldC> is Required ",  " <Head> ")
            txt<FieldU>.Focus()
            Return False
        End If
           <IsNotIdentity>
           <And>
           <IsNotNull>
           <And>
           <IsComboBox (Db)>
        If txt<FieldU>.SelectedIndex = -1 Then
            ec.ShowMessage(" <FieldC> is Required ",  " <Head> ")
            txt<FieldU>.Focus()
            Return False
        End If         
      </If Loop>
---------------------------------------<Comment>
        <If Loop>
           <IsNotIdentityCount>
        If _
           <ExitLoop>
        </If Loop>
        <If Loop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsNumericUpDown>
            txt<FieldU>.Text <> "" _
	            <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsTextBox>
            txt<FieldU>.Text <> "" _
	            <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsMaskedTextBox>
            txt<FieldU>.Text <> "" _
	            <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsMultilineTextBox>
            txt<FieldU>.Text <> "" _
	            <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsRichTextBox>
            txt<FieldU>.Text <> "" _
	            <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsComboBox (Db)>
             txt<FieldU>.SelectedIndex <> -1 _
	            <ExitLoop>
        </If Loop>
        <If Loop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsNumericUpDown>
           <And>
           <IsCurRow<>[0]>
            And txt<FieldU>.Text <> "" _
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsTextBox>
           <And>
           <IsCurRow<>[0]>
            And txt<FieldU>.Text <> "" _
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsMaskedTextBox>
           <And>
           <IsCurRow<>[0]>
            And txt<FieldU>.Text <> "" _
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsMultilineTextBox>
           <And>
           <IsCurRow<>[0]>
            And txt<FieldU>.Text <> "" _
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsRichTextBox>
           <And>
           <IsCurRow<>[0]>
            And txt<FieldU>.Text <> "" _
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsComboBox (Db)>
           <And>
           <IsCurRow<>[0]>
             And txt<FieldU>.SelectedIndex <> -1 _
           <IsNotIdentity>
           <And>
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsNumericUpDown>
             txt<FieldU>.Text <> "" _
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsTextBox>
             txt<FieldU>.Text <> "" _
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsMaskedTextBox>
             txt<FieldU>.Text <> "" _
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsMultilineTextBox>
             txt<FieldU>.Text <> "" _
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsRichTextBox>
             txt<FieldU>.Text <> "" _
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsComboBox (Db)>
             txt<FieldU>.SelectedIndex <> -1 _
           <ExitLoop>
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
             Then  
             Dim cls<OwnerU>_<TableU> As New <OwnerU>_<TableU>Class
           <ExitLoop>
        </If Loop>
        <If Loop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsNumericUpDown>
             cls<OwnerU>_<TableU>.<FieldU> = <Convert>(txt<FieldU>.Text)
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsTextBox>
             cls<OwnerU>_<TableU>.<FieldU> = <Convert>(txt<FieldU>.Text)
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsMaskedTextBox>
             cls<OwnerU>_<TableU>.<FieldU> = <Convert>(txt<FieldU>.Text)
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsMultilineTextBox>
             cls<OwnerU>_<TableU>.<FieldU> = <Convert>(txt<FieldU>.Text)
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsRichTextBox>
             cls<OwnerU>_<TableU>.<FieldU> = <Convert>(txt<FieldU>.Text)
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsComboBox (Db)>
             cls<OwnerU>_<TableU>.<FieldU> = <Convert>(txt<FieldU>.SelectedValue)
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsNumericUpDown>
             cls<OwnerU>_<TableU>.<FieldU> = <Convert>(txt<FieldU>.Text)
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsTextBox>
             cls<OwnerU>_<TableU>.<FieldU> = <Convert>(txt<FieldU>.Text)
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsMaskedTextBox>
             cls<OwnerU>_<TableU>.<FieldU> = <Convert>(txt<FieldU>.Text)
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsMultilineTextBox>
             cls<OwnerU>_<TableU>.<FieldU> = <Convert>(txt<FieldU>.Text)
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsRichTextBox>
             cls<OwnerU>_<TableU>.<FieldU> = <Convert>(txt<FieldU>.Text)
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsComboBox (Db)>
             cls<OwnerU>_<TableU>.<FieldU> = <Convert>(txt<FieldU>.SelectedValue)
           <ExitLoop>
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
             cls<OwnerU>_<TableU> = cls<OwnerU>_<TableU>Data.Select_Record(cls<OwnerU>_<TableU>)
             If Not cls<OwnerU>_<TableU> Is Nothing And Session("Det_Mode") Is "Add" Then
                  ec.ShowMessage(" Record already exists. ",  " <Head> ")
           <ExitLoop>
        </If Loop>
        <If Loop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsNumericUpDown>
                  txt<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsTextBox>
                  txt<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsMaskedTextBox>
                  txt<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsMultilineTextBox>
                  txt<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsRichTextBox>
                  txt<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsComboBox (Db)>
                  txt<FieldU>.Focus()
           <ExitLoop>
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsNumericUpDown>
                  txt<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsTextBox>
                  txt<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsMaskedTextBox>
                  txt<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsMultilineTextBox>
                  txt<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsRichTextBox>
                  txt<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsComboBox (Db)>
                  txt<FieldU>.Focus()
           <ExitLoop>
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
                  Return False
             End If
           <ExitLoop>
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
        End If
           <ExitLoop>
        </If Loop>
---------------------------------------<Comment>
        Return True
    End Function

    Private Sub butRecords_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butRecords.Click
        grd<TableU>.CurrentPageIndex = 0
        grd<TableU>.PageSize = System.Convert.ToInt32(cmbRecords.Text)
        LoadGrid<OwnerU>_<TableU>()
    End Sub

    Private Sub btnAddNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
        If pnlForm.Visible Then
        <If Loop>
           <IsCheckBox>
            Me.txt<FieldU>.Checked = False
           <IsNumericUpDown>
            Me.txt<FieldU>.Text = ""
           <IsDateTimePicker>
            Me.txt<FieldU>.Text = ""
           <IsTextBox>
            Me.txt<FieldU>.Text = ""
           <IsMaskedTextBox>
            Me.txt<FieldU>.Text = ""
           <IsMultilineTextBox>
            Me.txt<FieldU>.Text = ""
           <IsRichTextBox>
            Me.txt<FieldU>.Text = ""
           <IsPictureBox>
            Me.txt<FieldU>.Text = ""
           <IsComboBox>
            Me.txt<FieldU>.SelectedIndex = -1
           <IsComboBox (Db)>
            Me.txt<FieldU>.SelectedIndex = -1
        </If Loop>
        Else
            pnlForm.Visible = True
            pnlGrid.Visible = False
            pnlSave.Visible = True
            lblMode.InnerText = "Add new"
        End If
        btnSave.CommandArgument = "Add"
        Add()
    End Sub

    Private Sub grd<TableU>_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles grd<TableU>.ItemCommand
        If e.Item.ItemType = ListItemType.Pager Or _
            e.Item.ItemType = ListItemType.Header Then Exit Sub

        Dim btn As Button = CType(e.CommandSource, Button)
        If btn.Text = "Edit" Or btn.Text = "Delete" Then
    	<If Loop>
        	<IsPKey>
            Session("Det_<FieldU>") = e.Item.Cells(<ColNo> + <ColNo>).Text
    	</If Loop>
    	<If Loop>
        	<IsNotPKey>
        	<And>
        	<IsIdentity>
            Session("Det_<FieldU>") = e.Item.Cells(<ColNo> + <ColNo>).Text
        	<ExitLoop>
    	</If Loop>
    	<If Loop>
        	<IsNotIdentityCount>
        	<And>
        	<IsNotPKeyCount>
            Session("Det_<FieldU>") = e.Item.Cells(<ColNo> + <ColNo>).Text
        	<ExitLoop>
    	</If Loop>
            If btn.Text = "Edit" Then
                Edit()
            ElseIf btn.Text = "Delete" Then
                Delete()
            End If
            pnlForm.Visible = True
            pnlGrid.Visible = False
            If btn.Text = "Edit" Then
                lblMode.InnerText = "Edit record"
                pnlSave.Visible = True
            End If
            If btn.Text = "Delete" Then
                lblMode.InnerText = "Delete record"
                pnlDelete.Visible = True
            End If
        End If
            btnSave.CommandArgument = ""
    End Sub

    Private Sub grd<TableU>_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles grd<TableU>.SortCommand
        htmlHiddenSortExpression.Value = e.SortExpression & " " & GetSortDirection(e.SortExpression)
        LoadGrid<OwnerU>_<TableU>()
    End Sub

    Private Sub grd<TableU>_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles grd<TableU>.PageIndexChanged
        grd<TableU>.CurrentPageIndex = e.NewPageIndex
        LoadGrid<OwnerU>_<TableU>()
    End Sub

    Private Sub butYes_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butYes.Click
        Me.DeleteRecord()
    End Sub

    Private Sub butNo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butNo.Click
        pnlDelete.Visible = False
        pnlGrid.Visible = True
        pnlForm.Visible = False
        lblMode.InnerText = ""
    End Sub

    Private Sub butCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butCancel.Click
        pnlForm.Visible = False
        pnlSave.Visible = False
        pnlGrid.Visible = True
        lblMode.InnerText = ""
    End Sub

    Private Sub butOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butOK.Click
        pnlMessage.Visible = False
        pnlGrid.Visible = True
    End Sub

    Private Sub showMessage()
        lblMode.InnerText = ""
        pnlForm.Visible = False
        pnlSave.Visible = False
        pnlDelete.Visible = False
        pnlGrid.Visible = False
        pnlMessage.Visible = True
    End Sub

    Private Sub butShowAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butShowAll.Click
        txtSearch.Text = Nothing
        Session.Remove("dv<OwnerU>_<TableU>")
        LoadGrid<OwnerU>_<TableU>()
    End Sub

    Private Sub butSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butSearch.Click
	Try
            Session.Remove("dv<OwnerU>_<TableU>")
            If Not IsNothing(Session("dv<OwnerU>_<TableU>")) Then
                dv<OwnerU>_<TableU> = CType(Session("dv<OwnerU>_<TableU>"), DataView)
            Else ' DataView was not found in the Session.
                dv<OwnerU>_<TableU> = cls<OwnerU>_<TableU>Data.Search(System.Convert.To<DRType>(Session("Det_<DRField>")), cmbFields.Text, cmbCondition.Text, txtSearch.Text).DefaultView
            	Session("dv<OwnerU>_<TableU>") = dv<OwnerU>_<TableU>
            End If
            If dv<OwnerU>_<TableU>.Count > 0 Then
                dv<OwnerU>_<TableU>.Sort = htmlHiddenSortExpression.Value
                With grd<TableU>
                    .DataSource = dv<OwnerU>_<TableU>
                    .DataBind()
                End With
            Else
                With grd<TableU>
                    .DataSource = Nothing
                    .DataBind()
                End With
            End If
        Catch ex As Exception
	    ec.ShowMessage(ex.Message, " <Head> ")
        End Try
    End Sub

    Public Sub btnExport_Click(sender As System.Object, e As System.EventArgs) Handles btnExport.Click
        Try
            If ddlFile.SelectedValue = ".pdf" Then
                Dim dt As New DataTable()
                If Not String.IsNullOrEmpty(txtSearch.Text) Then
                    dt = cls<OwnerU>_<TableU>Data.Search(System.Convert.To<DRType>(Session("Det_<DRField>")), cmbFields.Text, cmbCondition.Text, txtSearch.Text)
                Else
                    dt = cls<OwnerU>_<TableU>Data.Select_Records(System.Convert.To<DRType>(Session("Det_<DRField>")))
                End If

                Dim pdfForm As New PDFform(dt, "<Head>", "Many")
                Dim document As Document = pdfForm.CreateDocument()
                Dim renderer As New PdfDocumentRenderer(True)
                renderer.Document = document
                renderer.RenderDocument()

                Dim stream As New MemoryStream()
                renderer.PdfDocument.Save(stream, False)

                Response.Clear()
                Response.ContentType = "application/" + ddlFile.SelectedItem.Text + ddlFile.SelectedValue
                Response.AddHeader("content-disposition", "attachment;filename=" + "Report" + ddlFile.SelectedValue)
                Response.BinaryWrite(stream.ToArray())
                Response.Flush()
                Response.[End]()
            Else
                Response.Clear()
                Response.Charset = ""
                Response.ContentEncoding = System.Text.Encoding.UTF8
                Response.Cache.SetCacheability(HttpCacheability.NoCache)
                Response.ContentType = "application/" + ddlFile.SelectedItem.Text + ddlFile.SelectedValue
                Response.AddHeader("content-disposition", "attachment;filename=" + "Report" + ddlFile.SelectedValue)

                Dim sw As New System.IO.StringWriter()
                Dim htw As New HtmlTextWriter(sw)
                Dim GVExport As New GridView()
                GVExport.DataSource = Session("dv<OwnerU>_<TableU>")
                GVExport.DataBind()
                GVExport.RenderControl(htw)

                Response.Write(sw)
                sw = Nothing
                htw = Nothing
                Response.Flush()
                Response.[End]()
            End If
        Catch
        End Try
    End Sub

    Private Function GetSortDirection(ByVal column As String) As String
        Dim sortDirection = "ASC"
        Dim sortExpression = TryCast(ViewState("SortExpression"), String)
        If sortExpression IsNot Nothing Then
            If sortExpression = column Then
                Dim lastDirection = TryCast(ViewState("SortDirection"), String)
                If lastDirection IsNot Nothing _
                  AndAlso lastDirection = "ASC" Then
                    sortDirection = "DESC"
                End If
            End If
        End If
        ViewState("SortDirection") = sortDirection
        ViewState("SortExpression") = column
        Return sortDirection
    End Function

End Class

